﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CssCommonLibrary
{
    public class CssClassNode:IComparable<CssClassNode>
    {
        public string Selector { get; set; }
        public List<CssProperty> Properties { get; set; }
        public CssClassNode(string selector, List<CssProperty> properties)
        {
            this.Selector = selector;
            this.Properties = properties;
        }

        public CssClassNode(string selector) : this(selector, new List<CssProperty>()) { }

        public bool HasAttribute
        {
            get
            {
                return this.Properties != null && this.Properties.Count > 0;
            }
        }

        public override string ToString()
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.AppendLine(this.Selector);
            strBuilder.AppendLine("{");
            foreach (var property in Properties)
                strBuilder.AppendLine(property.ToString());
            strBuilder.AppendLine("}");
            return strBuilder.ToString();
        }

        public void AddProperties(List<CssProperty> properties)
        {
            foreach (CssProperty property in properties)
                this.AddProperty(property);
        }

        public void AddProperty(CssProperty property)
        {
            Properties.Add(property);
        }

        public void AddProperty(string key, string value)
        {
            this.AddProperty(new CssProperty(key, value));
        }

        public static CssClassNode operator +(CssClassNode c1, CssClassNode c2)
        {
            c1.AddProperties(c2.Properties);
            return new CssClassNode(c1.Selector, c1.Properties);
        }

        public int CompareTo(CssClassNode other)
        {
            return String.Compare(this.Selector, other.Selector, false);
        }
    }
}
